package edu.msg.jthr.web.servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Candidate;
import edu.msg.jthr.backend.model.Comment;
import edu.msg.jthr.backend.model.Department;
import edu.msg.jthr.backend.model.Job;
import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.service.CandidateService;
import edu.msg.jthr.backend.service.DepartmentService;
import edu.msg.jthr.backend.service.JobService;
import edu.msg.jthr.backend.service.UserService;
import edu.msg.jthr.web.util.RoleAssembler;

/**
 * Servlet implementation class ListJobServlet
 */
@WebServlet("/ShowJobs")
public class ShowJobs extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(ShowJobs.class);
	private HttpSession session;
	@EJB
	private JobService jobService;
	@EJB
	private DepartmentService departmentService;
	@EJB
	private CandidateService candidateService;
	@EJB
	private UserService userService;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowJobs() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Exception handling
		session = request.getSession(true);
		User user = (User) session.getAttribute("activeUser");
		if (user == null) {
			user = userService.getUserByUserName(request.getUserPrincipal()
					.toString());
			String role = RoleAssembler.getRole(user.getUserGroups());
			session.setAttribute("role", role);
			session.setAttribute("activeUser", user);
		}
		List<Job> jobList = Collections.emptyList();
		try {
			jobList = jobService.getAll();
		} catch (ServiceException e) {
			LOG.error(e.getMessage(), e);
		}
		session.setAttribute("jobList", jobList);
		request.getRequestDispatcher("/pages/ShowJobs.jsp").forward(request,
				response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Exception handling
		session = request.getSession(true);
		Job job = new Job();
		try {
			job = jobService.getById(Long.parseLong(request
					.getParameter("jobId")));
		} catch (ServiceException e) {
			LOG.error(e.getMessage(), e);
		}
		if (request.getParameter("update") != null) {
			List<Department> departmentList = Collections.emptyList();
			try {
				departmentList = departmentService.getAll();
			} catch (ServiceException e) {
				LOG.error(e.getMessage(), e);
			}
			session.setAttribute("job", job);
			session.setAttribute("departmentList", departmentList);
			request.getRequestDispatcher("/pages/UpdateJob.jsp").forward(
					request, response);
		}
		if (request.getParameter("delete") != null) {
			try {
				jobService.delete(job);
			} catch (ServiceException e) {
				LOG.error(e.getMessage(), e);
			}
			doGet(request, response);
		}

		if (request.getParameter("addComment") != null) {
			session = request.getSession(true);
			String commentFromArea = request.getParameter("commentArea");

			Comment comment = new Comment();

			User user = (User) session.getAttribute("activeUser");
			comment.setUser(user);
			comment.setContent(commentFromArea);
			comment.setDate(Calendar.getInstance().getTime());
			job.getComments().add(comment);
			
			try {
				jobService.update(job);
			} catch (ServiceException e) {
				LOG.error(e.getMessage(), e);
			}

			doGet(request, response);
		}
		if (request.getParameter("recommend") != null) {
			List<Candidate> candidates = Collections.emptyList();
			try {
				candidates = candidateService.getAll();
			} catch (ServiceException e) {
				LOG.error(e.getMessage(), e);
			}
			session.setAttribute("job", job);
			session.setAttribute("candidates", candidates);
			request.getRequestDispatcher("/pages/RecommendCandidate.jsp")
					.forward(request, response);
		}
		if (request.getParameter("viewrecommended") != null) {
			session.setAttribute("job", job);
			session.setAttribute("recomCandidateList",
					job.getRecomendedCandidates());
			request.getRequestDispatcher("/pages/ShowRecommendedCandidates.jsp")
					.forward(request, response);

		}
	}

}
